$(function(){
	
	type = sessionStorage['type'];
	productId = sessionStorage['productId'];
	productName = sessionStorage['productName'];
	productPrice = sessionStorage['type'];
	image = sessionStorage['image'];
	productIds = sessionStorage['productIds'];
	shopCartIds = sessionStorage['shopCartIds'];
	total = sessionStorage['total'];
	comment = sessionStorage['comment'];
	$("#gridPage").mobileGrid({pageSize:5,currentPageIndex:1,userId:sessionStorage['userId'],server:localStorage['APP_AJAX_PATH']+"/drinkingBar/view/chooseAddress.jsp.findAddressByUserId"});
	$("#chooseBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		if(type == 3){
		 androidLocal({"addressId":rows[0].id});
		 androidLocal({"productId":productId});
		 androidLocal({"productName":productName});
		 androidLocal({"productPrice":productPrice});
		 androidLocal({"image":image});
		 androidLocal({"comment":comment});
		 window.location="buyProduct.html";
		}
		if(type == 4){
			 androidLocal({"addressId":rows[0].id});
			 androidLocal({"shopCartIds":shopCartIds});
			 androidLocal({"total":total});
			 androidLocal({"productIds":productIds});
			 androidLocal({"comment":comment});
		 window.location="buyShopCart.html";
		}
		});
	$("#addBtn").bind("tap",function(){
		 	androidLocal({"type":2});
			window.location="addAddress.html";
   });
	$("#updateBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		 	androidLocal({"type":2});
			androidLocal({"addressId":rows[0].id});
			window.location="updateAddress.html";
	  	
   });
	$("#delBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		 Newtouch.Ajax.sendParams({addressId:rows[0].id},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/chooseAddress.jsp.deleteAddress",function(){
         	$(window).mobileMsgBox({msgText:"删除成功",delayTime:500});
         	if(type == 3){
       		 androidLocal({"addressId":rows[0].id});
       		 androidLocal({"productId":productId});
       		 androidLocal({"productName":productName});
       		 androidLocal({"productPrice":productPrice});
       		 androidLocal({"image":image});
       		 androidLocal({"comment":comment});
       		 window.location="chooseAddress.html";
       		}
       		if(type == 4){
       			 androidLocal({"addressId":rows[0].id});
       			 androidLocal({"shopCartIds":shopCartIds});
       			 androidLocal({"total":total});
       			 androidLocal({"productIds":productIds});
       			 androidLocal({"comment":comment});
       			 window.location="chooseAddress.html";
       		}
         	},function(err){
	    	});
	  	
   });
	$("#setDefaultBtn").bind("tap",function(){
		var rows=$("#gridPage").selectedRecords();
		 if (!rows || rows.length != 1) {
			 $(window).mobileMsgBox({msgText:"请选择一行",delayTime:500});
				return;
			    }
		 Newtouch.Ajax.sendParams({addressId:rows[0].id,userId:sessionStorage['userId']},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/chooseAddress.jsp.setDefaultAddress",function(){
         	$(window).mobileMsgBox({msgText:"设置成功",delayTime:500});
         	 window.location="chooseAddress.html";
         	if(type == 3){
       		 androidLocal({"addressId":rows[0].id});
       		 androidLocal({"productId":productId});
       		 androidLocal({"productName":productName});
       		 androidLocal({"productPrice":productPrice});
       		 androidLocal({"image":image});
       		 androidLocal({"comment":comment});
       		 window.location="chooseAddress.html";
       		}
       		if(type == 4){
       			androidLocal({"addressId":rows[0].id});
       			 androidLocal({"shopCartIds":shopCartIds});
       			 androidLocal({"total":total});
       			 androidLocal({"productIds":productIds});
       			 androidLocal({"comment":comment});
       			 window.location="chooseAddress.html";
       		}
		 },function(err){
	    	});
	  	
   });
	

});

function address(cell,data){
	if(data.addrState == 1){
		var html='【默认】'+data.deliveryPersonNm+data.deliveryMobile+'</br>'+data.city_name+data.area_name+data.street_name+data.detail+'';
		cell.html(html);
	}else{
		var html=''+data.deliveryPersonNm+data.deliveryMobile+'</br>'+data.city_name+data.area_name+data.street_name+data.detail+'';
		cell.html(html);	
	}
	
}
function editOrDelete(cell,data){
	var html='<b class="ui-mobile-grid-title">操作</b><a class="btn" onclick=editRecord("'+data.id+'")><img src="resource/image/gouwuche_32.png"></img>';
	cell.html(html);
}

function editRecord(id){         
	Newtouch.Ajax.sendParams({productId:id},localStorage['APP_AJAX_PATH']+"/drinkingBar/view/chooseAddress.jsp.addProToCart",function(data){	
		$(window).mobileMsgBox({msgText:"添加购物车成功",delayTime:500});
	},function(err){
		$(window).mobileMsgBox({msgText:"添加购物车失败",delayTime:500});
	});
    
}
var androidLocal=function(p){
	for(var k in p){
		sessionStorage[k]=p[k];
	}
	
}


